#ifndef TESTDIRECTO_H
#define TESTDIRECTO_H

#include <iostream>
#include <stdio.h>

#include "../directoHash/extendiblehash.cpp"
#include "../persistencia/serializable.h"
#include "../persistencia/keys/typekey.h"

class UIntKey : public TypeKey<u_int32_t> {
    public:
        UIntKey(){}
        UIntKey(u_int32_t id) : TypeKey<u_int32_t>(id) {}

        virtual u_int32_t hashcode(){
            return id;
        }
};

class TestRecordInt : public Serializable {
    public:
        TestRecordInt() { }

        TestRecordInt(u_int32_t id){
            key.set(id);
        }
        UIntKey& getKey(){
            return key;
        }
        virtual void read(BinaryStream& in) {
            key.read(in);
        }
        virtual void write(BinaryStream& out) {
            key.write(out);
        }
        virtual std::string toString() {
            std::stringstream sout;
            sout << key.get();
            return sout.str();
        }
    private:
        UIntKey key;
};

void testHash() {
    {
        ExtendibleHash<UIntKey, TestRecordInt>  hash;
        if ( hash.open("prueba.bin", 24) ||  hash.create("prueba.bin", 24)) {
            hash.add(TestRecordInt(551));
            hash.add(TestRecordInt(539));
            hash.add(TestRecordInt(41));
            hash.add(TestRecordInt(177));
            hash.add(TestRecordInt(398));
            hash.add(TestRecordInt(190));
            hash.add(TestRecordInt(110));
            hash.add(TestRecordInt(942));
            hash.add(TestRecordInt(68));
            hash.add(TestRecordInt(436));
            hash.add(TestRecordInt(516));
            hash.add(TestRecordInt(788));
            hash.add(TestRecordInt(786));
            std::cout << hash.toString() << std::endl;
        }
    }

    {
        ExtendibleHash<UIntKey, TestRecordInt> hash;
        if (hash.open("prueba.bin", 24)) {
            std::cout << "Baja 786" << std::endl;
            hash.remove(UIntKey(786));
            std::cout << hash.toString() << std::endl;

            std::cout << "Alta 278" << std::endl;
            hash.add(TestRecordInt(278));
            std::cout << hash.toString() << std::endl;

            std::cout << "Alta 151" << std::endl;
            hash.add(TestRecordInt(151));
            std::cout << hash.toString() << std::endl;
        }
    }

    remove("prueba.bin");
}

#endif // TESTDIRECTO_H
